<?php
/**
 * Created by PhpStorm.
 * User: 共赢岛
 * Date: 2023/12/19
 * Time: 17:03
 */

namespace App\Services\LifeCycle;


use App\Models\CarPackageOrder;
use App\Models\CarPackageOrderAttr;
use App\Models\CarPackageUsedLog;
use App\Models\User;
use App\Models\UserCar;
use Illuminate\Support\Facades\Auth;

class UserService
{
    public static function userInfo($user_id)
    {
        return User::find($user_id);
    }

    public static function carOne($user_id = 0)
    {
        if ($user_id) {
            $user = self::userInfo($user_id);
        } else {
            $user = Auth::user();
        }

        $car = UserCar::where('user_id', $user->id)->first();

        if ($car) {
            if ($car->order_id) {
                $order = OrderService::detail($car->order_id);
                $car->transact_name = $order->transact_name;
            }

            $car->name = $user->name;
            $car->phone = $user->phone;
            $car->id_card = $user->id_card;
        }

        return $car;
    }

    public static function saveUserInfo($param = [])
    {
        $user = Auth::user();
        $user_car = UserCar::where('user_id', $user->id)->first();

        $car = CarService::one($param['car_id']);

        $param['brand_id'] = $car->brand_id;

//        $param['car_license'] = json_decode($param['car_license'], true);
//        $param['car_license'] = $param['car_license'][0]['url'];

        if ($param['order_id']) {
            $order = OrderService::detail($param['order_id']);

            if (!$order) {
                return false;
            }

            $order->transact_name = $param['transact_name'];
            $order->user_phone = $param['phone'];
            $order->user_name = $param['name'];
            $order->save();
        }

        if ($user_car) {
            $user_car->car_id = $param['car_id'];
            $user_car->car_number = $param['car_number'] ?? '';
            $user_car->car_engine = $param['car_engine'];
            $user_car->car_frame = $param['car_frame'];
            $user_car->car_license = $param['car_license'] ?? '';
            $user_car->save();
        } else {
            $param['user_id'] = $user->id;
            UserCar::create($param);
        }

        $user->name = $param['name'];
        $user->id_card = $param['id_card'];
        $user->phone = $param['phone'];

        $user->save();

        return true;
    }

    public static function verifyOldUser($phone)
    {
        if (!$phone) {
            return false;
        }

        $user = Auth::user();

        if ($user->phone) {
            return false;
        }

        $old_user = User::where('phone', $phone)->first();

        if ($old_user->app_open_id && $old_user->union_id) {
            return false;
        }

        CarPackageOrder::where('user_id', $old_user->id)->update([
           'user_id' => $user->id
        ]);

        CarPackageOrderAttr::where('user_id', $old_user->id)->update([
            'user_id' => $user->id
        ]);

        CarPackageUsedLog::where('user_id', $old_user->id)->update([
            'user_id' => $user->id
        ]);

        UserCar::where('user_id', $old_user->id)->update([
            'user_id' => $user->id
        ]);

        $user->phone = $old_user->phone;
        $old_user->phone = '';
        $user->save();
        $old_user->save();

        return true;
    }
}